package com.yc.mvc.action;

import com.yc.mvc.bean.Result;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

@RestController
public class UploadAction {

    @PostMapping(value = "upload.do",produces = MediaType.APPLICATION_JSON_VALUE +";charset=utf-8")
    // MultipartFile SpringMVC 封装的文件对象
    public Result upload(MultipartFile file) throws IOException {
        file.getOriginalFilename(); // 文件名  head.jpg
        file.getName();    // 文件字段名   <input name="file" type="file">
        file.getBytes();    // 获取文件字节数组
        file.getSize();// 获取大小
        file.getContentType(); // 获取 http 请求头中 Content-Type 类型
        String diskpath = "D:/_文件柜/97/upload/" + file.getOriginalFilename();
        file.transferTo(new File(diskpath));  // 将上传的文件保存到本地文件
        return new Result(1,"文件上传成功",diskpath);
    }

    @PostMapping(value = "uploadImg.do",produces = MediaType.APPLICATION_JSON_VALUE +";charset=utf-8")
    // MultipartFile SpringMVC 封装的文件对象
    public Result uploadImg(MultipartFile file) throws IOException {
        String diskpath = "D:/_文件柜/97/upload/" + file.getOriginalFilename();
        String webpath = "upload/" + file.getOriginalFilename();
        file.transferTo(new File(diskpath));  // 将上传的文件保存到本地文件
        return new Result(1,"文件上传成功",webpath);
    }

}
